package com.example.demo.controllers;

import com.example.demo.entities.User;
import com.example.demo.entities.User1;
import com.example.demo.entities.User2;
import com.example.demo.services.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RequestMapping("user")
@RestController
public class UserController {

    private final UserService userService;

    @Autowired
    public UserController(UserService userService) {
        this.userService = userService;
    }

    @RequestMapping("get_all")
    public List<User> get(){
        return  userService.queryUsers();
    }

    @RequestMapping("get")
    public  List<User1> getByName(@RequestParam String name) {
        return  userService.getUser(name);
    }

    @RequestMapping("delete/{id}")
    public String deleteUser(@PathVariable long id){
        if(  userService.delete(id)){
            return  "success";
        }
        return  "fail";
    }

    @RequestMapping("add")
    public String Add(){
        User1 user1 = new User1();
        user1.setUsername("33");
        user1.setPassword("33");
        if(userService.add(user1)){
            return  "success";
        }
        return  "fail";
    }

    @RequestMapping("get_by_name")
    public List<User2> findByUsername(@RequestParam String name){
        return userService.findByUsername(name);
    }
}
